package boc.sx.gateway.core.strategy;

import boc.sx.gateway.core.ServiceNode;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;

/*
 * @author zmx
 * @date 2022/8/9 15:55
 */

public interface LoadBalanceStrategy extends Function<ServiceNode,String> {

    String vote(ServiceNode serviceNode);

    default String apply(ServiceNode serviceNode){
       return vote(serviceNode);
    }

    // ip地址对应访问次数
    public static final Map<String,Integer> urlVisitCount = new ConcurrentHashMap<>();


    default int getCount(String url){
       return urlVisitCount.getOrDefault(url,0);
    }

    default void cleanCounter(){
        urlVisitCount.clear();
    }

    default void addCount(String url){
        urlVisitCount.put(url,urlVisitCount.getOrDefault(url,0)+1);
    }

}
